//  Gmof - A meta-object facility library based on OMG's EMOF 
//
//  Copyright (C) 2007 Peter Hagg <peter.hagg@iki.fi>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
#include "ReflectiveCollection.h"
#include "Object.h"
#include "IllegalArgumentException.h"
#include "ReflectiveSequence.h"

Gmof::ReflectiveCollection::ReflectiveCollection() : Gmof::Object()
{
	// Empty
}

Gmof::ReflectiveCollection::~ReflectiveCollection()
{
	clear();
}

bool
Gmof::ReflectiveCollection::add(Gmof::Object* object)
{
	std::vector<Gmof::Object*>::iterator iter = objects.begin();
	for (; iter != objects.end(); iter++)
	{
		Gmof::Object* tmp = dynamic_cast<Gmof::Object*>(*iter);
		if (object == tmp)
			throw IllegalArgumentException("Object already in collection");
	}
	
	objects.push_back(object);
	
	return true;	
}

bool
Gmof::ReflectiveCollection::add_all(Gmof::ReflectiveSequence* objects)
{
	for (int index = 0; index < objects->size(); index++)
	{
		Gmof::Object* object = objects->get(index);
		
		add(object);
		object->reference();
	}

	return true;
}

void
Gmof::ReflectiveCollection::clear()
{
	std::vector<Gmof::Object*>::iterator iter = objects.begin();
	for (; iter != objects.end(); iter++)
	{
		Gmof::Object* object = dynamic_cast<Gmof::Object*>(*iter);
		if (object != NULL)
			object->unreference();
	}

	objects.clear();
}

bool
Gmof::ReflectiveCollection::remove(Gmof::Object* object)
{
	std::vector<Gmof::Object*>::iterator iter = objects.begin();
	for (; iter != objects.end(); iter++)
	{
		Gmof::Object* tmp = dynamic_cast<Gmof::Object*>(*iter);
		if (object == tmp)
		{
			tmp->unreference();
			objects.erase(iter);
			return true;
		}
	}
	
	return false;
}

int
Gmof::ReflectiveCollection::size() const
{
	return (int) objects.size();
}

// End of file
